﻿@inject IStringLocalizer<ValidateForms> Localizer

<ValidateForm @ref="FooForm" Model="@Model" OnValidSubmit="@OnValidSetError">
    <div class="row g-3">
        <div class="col-12 col-sm-6">
            <BootstrapInput @bind-Value="@Model.Name" />
        </div>
        <div class="col-12 col-sm-6">
            <BootstrapInputNumber @bind-Value="@Model.Count" />
        </div>
        <div class="col-12 col-sm-6">
            <DateTimePicker @bind-Value="@Model.DateTime" />
        </div>
        <div class="col-12 col-sm-6">
            <Select @bind-Value="@Model.Education" />
        </div>
        <div class="col-12">
            <CheckboxList @bind-Value="@Model.Hobby" Items="@Hobbys" />
        </div>
        <div class="col-12">
            <Button ButtonType="@ButtonType.Submit" Text="@Localizer["ValidateFormsSubmitButtonText"]" />
        </div>
    </div>
</ValidateForm>

@code {
    [NotNull]
    private Foo? Model { get; set; }

    [NotNull]
    private IEnumerable<SelectedItem>? Hobbys { get; set; }

    [NotNull]
    private ValidateForm? FooForm { get; set; }

    private Task OnValidSetError(EditContext context)
    {
        FooForm.SetError<Foo>(f => f.Name, Localizer["DatabaseExistLog"]);
        return Task.CompletedTask;
    }

    /// <summary>
    /// OnInitializedAsync 方法
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

    // 切换线程 模拟异步通过 webapi 加载数据
        await Task.Yield();

        Model = new() { Name = "Name", Education = EnumEducation.Primary, DateTime = DateTime.Now };
    }
}
